#ifndef _adc_api_h
#define _adc_api_h
/*hardware connection
USBDM   ----AD0-----PA2
USBDP   ----AD1-----PA3
VIN_OVP ----AD7-----PA16
VOLTAGE ----AD8-----PB12
CURRENT ----AD11----PB18
*/
//--------include files list----------------------------------------------------
#include "adc.h"
#include "pmc.h"
#include "sysclk.h"

//--------MARCO-----------------------------------------------------------------
//define ENABLE_ADC_TAG
#define ADC_SEQUENCE_ENABLE
//#define ADC_GAIN_ENABLE
//#define ADC_OFFSET_ENABLE
//#define ADC_POWER_SAVE_ENABLE
#define ADC_PDC_ENABLE

#define ADC_BUFFER_SIZE                         5
#define ADC_TOTAL_CHANNEL_NUMS                  5
#define ADC_CH_USBDP                            ADC_CHANNEL_1
#define ADC_CH_USBDM                            ADC_CHANNEL_0
#define ADC_CH_OVP                              ADC_CHANNEL_7
#define ADC_CH_CURRENT                          ADC_CHANNEL_11                        
#define ADC_CH_VOLTAGE                          ADC_CHANNEL_8

//adc clock frequency is 1--20MHz,ADCClock = MCK/((PRESCAL+1) * 2)
#define ADC_CLOCK_FREQ                          6400000

//adc startup time 0--15 
//0 SUT0 0 periods of ADCClock
//1 SUT8 8 periods of ADCClock
//2 SUT16 16 periods of ADCClock
//3 SUT24 24 periods of ADCClock
//4 SUT64 64 periods of ADCClock
//5 SUT80 80 periods of ADCClock
//6 SUT96 96 periods of ADCClock
//7 SUT112 112 periods of ADCClock
//8 SUT512 512 periods of ADCClock
//9 SUT576 576 periods of ADCClock
//10 SUT640 640 periods of ADCClock
//11 SUT704 704 periods of ADCClock
//12 SUT768 768 periods of ADCClock
//13 SUT832 832 periods of ADCClock
//14 SUT896 896 periods of ADCClock
//15 SUT960 960 periods of ADCClock
#define ADC_STARTUP_TIME                        15

//adc transfer time 0--3,Transfer Period = (TRANSFER * 2 + 3)*ADCClock periods
#define ADC_TRANSFER_TIME                       3

//adc setting time ADC_SETTLING_TIME_0--ADC_SETTLING_TIME_3
//0 AST3 3 periods of ADCClock
//1 AST5 5 periods of ADCClock
//2 AST9 9 periods of ADCClock
//3 AST17 17 periods of ADCClock
#define ADC_SETTING_TIME                         ADC_SETTLING_TIME_3

//adc tracking time 0--15,Tracking Time = (TRACKTIM + 1) * ADCClock periods.
#define ADC_TRACKING_TIME                        15           

//--------global veriables------------------------------------------------------
extern uint16_t adc_data_buf[ADC_TOTAL_CHANNEL_NUMS];
extern uint8_t  adc_samp_times;
//--------functions list--------------------------------------------------------
void ADC_init_adc(void);
uint32_t ADC_prepare_buffer(Adc * p_adc, uint16_t * p_s_buffer, uint32_t ul_size);
void ADC_set_sequence(Adc *p_adc, uint8_t* ch_list,
		uint8_t uc_num);
bool get_adc_result(bool b_force_reset);
#define get_cur()                 (adc_data_buf[4])
#define get_d_plus_volt()         (adc_data_buf[1])
#define get_d_minus_volt()        (adc_data_buf[0])
#define get_battery_volt()        (adc_data_buf[3])
#define get_input_voltage()       (adc_data_buf[2]);

//void start_check_adc(void);
void reset_check_adc(void);
#endif
